package generator

import (
	"code/config"
	"code/utils"
	"fmt"
	"reflect"
	"strings"

	"github.com/fatih/color"
)

// Config struct for project configuration
// 定义全局颜色变量
var (
	red   = color.New(color.FgRed).Add(color.Bold)
	green = color.New(color.FgGreen)
	blue  = color.New(color.FgBlue)
	// 可以根据需要添加更多颜色
)

// Method struct for handling logic
type Method struct {
	Name        string
	HttpMethod  string
	Path        string
	ReturnType  string
	Description string
	Params      string
	Logic       string
}

//匹配好要修改的java文件
func buildFilePath(config *config.Config) {
	config.ControllerFilePath = fmt.Sprintf("%s/%s/%s/%sController.%s",
		config.RootPath, config.ModPath, config.ControllerPath, config.Name, config.FileType)
	config.ServiceFilePath = fmt.Sprintf("%s/%s/%s/I%sService.%s",
		config.RootPath, config.ModPath, config.ServicePath, config.Name, config.FileType)
	config.ServiceImplFilePath = fmt.Sprintf("%s/%s/%s/%sServiceImpl.%s",
		config.RootPath, config.ModPath, config.ServiceImplPath, config.Name, config.FileType)
	config.MapperFilePath = fmt.Sprintf("%s/%s/%s/%sMapper.%s",
		config.RootPath, config.ModPath, config.MapperPath, config.Name, config.FileType)
	config.DtoFilePath = fmt.Sprintf("%s/%s/%s/%s.%s",
		config.RootPath, config.ModPath, config.DtoPath, config.Name, config.FileType)
	config.MapperXMLFilePath = fmt.Sprintf("%s/%s/%s/%sMapper.xml",
		config.RootPath, config.ModPath, config.MapperXMLPath, config.Name)
	//config.ControllerFilePath = config.RootPath + "/" + config.ModPath + "/" + config.ControllerPath  + "/" + config.Name + "Controller."+config.FileType
}

func printConfig(config config.Config) {
	val := reflect.ValueOf(config)
	typ := val.Type()

	for i := 0; i < val.NumField(); i++ {
		field := typ.Field(i)
		value := val.Field(i)

		fmt.Printf("%s: %v\n", field.Name, value.Interface())
	}
}

// 检查所有文件

// GenerateFiles generates files based on the config
func GenerateFiles(config config.Config) {

	//大驼峰 name 和action tag
	//config.Name = utils.ToPascalCase(config.Name)
	config.Action = utils.ToPascalCase(config.Action)
	config.Tag = utils.ToPascalCase(config.Tag)

	//初始化标记tag
	config.StartTag = "TAG_START:" + config.Name + "_" + config.Tag + "_" + config.Type
	config.EndTag = "TAG_END:" + config.Name + "_" + config.Tag + "_" + config.Type

	//构建文件地址
	buildFilePath(&config)
	fmt.Println("开始生成文件")
	//fmt.Printf("Config: %+v\n", config)
	//printConfig(config)

	//检测文件是否存在
	filePaths := []string{
		config.ControllerFilePath,
		config.ServiceFilePath,
		config.ServiceImplFilePath,
		config.MapperFilePath,
		config.DtoFilePath,
		config.MapperXMLFilePath,
	}

	validateFiles(filePaths)

	//重复代码检测
	red := color.New(color.FgRed).Add(color.Bold)
	//green := color.New(color.FgGreen)
	err := CheckDuplicateCode(filePaths, &config)
	if err != nil {
		// 将错误信息按行分割
		lines := strings.Split(err.Error(), "\n")
		red.Println("错误:")
		for _, line := range lines {
			red.Println(line)
		}
		return
	}

	//打印查看变量配置
	if config.Action == "Test" {
		printConfig(config)
	}

	//插入代码
	if config.Action == "Add" {
		//插入测试的代码方便后面删除用途
		//示例  go run main.go -name BaiSci -tag new2 -type list -table bai_sci -api /list2 -apitype GET -action Add
		//GenerateFilesTest(config)
		if config.CodeType == "interface" {
			GenerateInterFaceFiles(config)
		}
		if config.CodeType == "dto" {
			GenerateDtoFiles(config)
		}

	}

	//移除代码
	//示例  go run main.go -name BaiSci -tag new2 -type list -table bai_sci -api /list2 -apitype GET -action Delete
	if config.Action == "Delete" {

		for _, filePath := range filePaths {
			err := RemoveCodeBetweenTags(filePath, &config)
			if err != nil {
				fmt.Printf("错误处理文件 %s: %v\n", filePath, err)
				return
			}
		}
	}

}
